package com.streetlight.lampcursor.repository

import com.streetlight.lampcursor.model.LightMode
import com.streetlight.lampcursor.model.LightStatus
import com.streetlight.lampcursor.model.ScheduleData
import com.streetlight.lampcursor.model.SettingsData
import com.streetlight.lampcursor.model.StreetLightData
import com.streetlight.lampcursor.network.StreetLightApi
import kotlinx.coroutines.delay

class StreetLightRepository {
    
    private val api = StreetLightApi()
    
    // 模拟数据，实际应用中应该从API获取
    private var currentStatus = LightStatus.OFF
    private var currentMode = LightMode.MANUAL
    private var currentBrightness = 50
    private var isOnline = false

    suspend fun turnOnLight(): Boolean {
        return try {
            delay(1000) // 模拟网络延迟
            currentStatus = LightStatus.ON
            api.turnOnLight()
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun turnOffLight(): Boolean {
        return try {
            delay(1000)
            currentStatus = LightStatus.OFF
            api.turnOffLight()
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setAutoMode(): Boolean {
        return try {
            delay(1000)
            currentMode = LightMode.AUTO
            currentStatus = LightStatus.AUTO
            api.setAutoMode()
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setManualMode(): Boolean {
        return try {
            delay(1000)
            currentMode = LightMode.MANUAL
            api.setManualMode()
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setBrightness(brightness: Int): Boolean {
        return try {
            delay(500)
            currentBrightness = brightness.coerceIn(0, 100)
            api.setBrightness(brightness)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setAutoBrightness(enabled: Boolean): Boolean {
        return try {
            delay(500)
            api.setAutoBrightness(enabled)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setMotionSensor(enabled: Boolean): Boolean {
        return try {
            delay(500)
            api.setMotionSensor(enabled)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun setWeatherAdaptation(enabled: Boolean): Boolean {
        return try {
            delay(500)
            api.setWeatherAdaptation(enabled)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun getLightStatus(): LightStatus {
        return try {
            delay(500)
            api.getLightStatus()
        } catch (e: Exception) {
            currentStatus
        }
    }

    suspend fun getLightMode(): LightMode {
        return try {
            delay(500)
            api.getLightMode()
        } catch (e: Exception) {
            currentMode
        }
    }

    suspend fun getBrightness(): Int {
        return try {
            delay(500)
            api.getBrightness()
        } catch (e: Exception) {
            currentBrightness
        }
    }

    suspend fun getStreetLightData(): StreetLightData {
        return try {
            delay(1000)
            api.getStreetLightData()
        } catch (e: Exception) {
            // 返回模拟数据
            StreetLightData(
                id = "SL001",
                status = currentStatus,
                mode = currentMode,
                brightness = currentBrightness,
                powerConsumption = 100.0,
                workingHours = 8.5,
                temperature = 25.0,
                voltage = 220.0,
                current = 0.5,
                isOnline = isOnline,
                lastUpdate = System.currentTimeMillis()
            )
        }
    }

    suspend fun getSchedules(): List<ScheduleData> {
        return try {
            delay(1000)
            api.getSchedules()
        } catch (e: Exception) {
            // 返回模拟数据
            listOf(
                ScheduleData(
                    id = "SCH001",
                    name = "工作日",
                    startTime = "18:00",
                    endTime = "06:00",
                    brightness = 80,
                    isEnabled = true,
                    daysOfWeek = listOf(1, 2, 3, 4, 5)
                ),
                ScheduleData(
                    id = "SCH002",
                    name = "周末",
                    startTime = "19:00",
                    endTime = "07:00",
                    brightness = 60,
                    isEnabled = true,
                    daysOfWeek = listOf(0, 6)
                )
            )
        }
    }

    suspend fun getSettings(): SettingsData {
        return try {
            delay(500)
            api.getSettings()
        } catch (e: Exception) {
            // 返回模拟数据
            SettingsData(
                autoBrightness = true,
                motionSensor = false,
                weatherAdaptation = true,
                serverAddress = "192.168.1.100",
                deviceId = "SL001"
            )
        }
    }

    suspend fun addSchedule(schedule: ScheduleData): Boolean {
        return try {
            delay(1000)
            api.addSchedule(schedule)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun updateSchedule(schedule: ScheduleData): Boolean {
        return try {
            delay(1000)
            api.updateSchedule(schedule)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun deleteSchedule(scheduleId: String): Boolean {
        return try {
            delay(1000)
            api.deleteSchedule(scheduleId)
            true
        } catch (e: Exception) {
            false
        }
    }

    suspend fun updateSettings(settings: SettingsData): Boolean {
        return try {
            delay(1000)
            api.updateSettings(settings)
            true
        } catch (e: Exception) {
            false
        }
    }
} 